package www.ash.com;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

/**
 * @author : Chen Chao
 * @description 蛇的构造类
 * @encoding : UTF-8
 * @date : 2021/9/30 17:12
 * @file : mySnake.www.ash.com.MySnake.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MySnake {
    /**
     * 蛇身体list
     */
    private ArrayList<SnakeBody> snakeBodies = new ArrayList<>();
    /**
     * 蛇身体的大小
     */
    private Integer size;
    /**
     * 蛇头x轴方向，向上为1，向下为0
     */
    private Integer xDirection;
    /**
     * 蛇头y轴方向，向上为1，向下为0
     */
    private Integer yDirection;

    /**
     * 蛇吃食物方法
     */
    public void eatFood() {
        if (snakeBodies.size() > 0) {
            SnakeBody head = snakeBodies.get(snakeBodies.size() - 1);
            SnakeBody newBody = new SnakeBody(head.getX() + xDirection, head.getY() + yDirection);
            snakeBodies.add(newBody);
        }
    }

    /**
     * 蛇移动方法
     */
    public void move() {
        if (snakeBodies.size() > 0) {
            eatFood();
            snakeBodies.remove(0);
            snakeBodies.forEach(System.out::println);
            System.out.println("+++++++++++++++++++++");
        }
    }

    /**
     * @author chen chao
     * @description 蛇转向方法
     * @param x 要转的x轴方向
     * @param y 要转的y轴方向
     */
    public void changeDirection(Integer x,Integer y) {
        /*先判断，是否只转一个方向*/
        if(x!=0&&y!=0){
            return;
        }
        /*只转一个方向，在确定赚那个方向*/
        if(x!=0&&getXDirection()==0){
            setXDirection(x);
            setYDirection(0);
        }
        if(y!=0&&getYDirection()==0){
            setYDirection(y);
            setXDirection(0);
        }
    }
}
